<?php

require 'InternalVersionSystemRepo.php';

class InternalVersionSystem {

    protected function parseOptions($options=array()) {
        $result = array();
        foreach($options as $option) {
            $pattern = '#^--(.+?)=(.+)$#isu';
            if (preg_match($pattern, $option, $m) === 1) {
                $name  = $m[1];
                $value = $m[2];
                $result[$name] = $value;
            }
        }
        return $result;
    }

    public function commandDuplicate($options = array()) {
        $parseOptions = $this->parseOptions($options);
        if (array_key_exists('workdir', $parseOptions)) {
            $originalSource = $parseOptions['workdir'];
        } else {
            $originalSource = getcwd();
        }

        $internalVersionSystemRepo = new InternalVersionSystemRepo();
        $listOriginalSource = $internalVersionSystemRepo->makeIndexOriginalSource($originalSource);
        foreach($listOriginalSource as $hash => $names) {
            if (is_array($names) and (count($names) > 1)) {
                echo '[P] ' . $hash . PHP_EOL;

                foreach($names as $name) {
                    echo ' => ' . $name . PHP_EOL;
                }

                echo PHP_EOL;
            }

        }
    }

    public function commandStat($options = array()) {
        $parseOptions = $this->parseOptions($options);
        if (array_key_exists('workdir', $parseOptions)) {
            $originalSource = $parseOptions['workdir'];
        } else {
            $originalSource = getcwd();
        }

        $result = array();
        $internalVersionSystemRepo = new InternalVersionSystemRepo();
        $listOriginalSource = $internalVersionSystemRepo->makeIndexOriginalSource($originalSource);
        $origList = $internalVersionSystemRepo->convertIndexToList($listOriginalSource);

        $listRepoSource = $internalVersionSystemRepo->loadRepo($originalSource, 'shapshot-201210021935');
        $repoList = $internalVersionSystemRepo->convertIndexToList($listRepoSource);

        // Add list
        $addList = array_diff(array_keys($origList), array_keys($repoList));
        foreach($addList as $name) {
            echo '[A] ' . $name . PHP_EOL;
        }

        // Remove list
        $removeList = array_diff(array_keys($repoList), array_keys($origList));
        foreach($removeList as $name) {
            echo '[D] ' . $name . PHP_EOL;
        }
        echo PHP_EOL;

        // Change list
        $changeList = array_intersect(array_keys($repoList), array_keys($origList));
        foreach($changeList as $name) {
            if ($repoList[$name] !== $origList[$name]) {
                echo '[M] ' . $name . PHP_EOL;
            }
        }
        echo PHP_EOL;
        
    }

    public function commandSnapshot($options = array()) {
        $parseOptions = $this->parseOptions($options);
        if (array_key_exists('workdir', $parseOptions)) {
            $originalSource = $parseOptions['workdir'];
        } else {
            $originalSource = getcwd();
        }

        $internalVersionSystemRepo = new InternalVersionSystemRepo();
        $listOriginalSource = $internalVersionSystemRepo->iterateOriginalSource($originalSource);
        $internalVersionSystemRepo->saveRepo($originalSource, $listOriginalSource);
    }

    public function commandHelp($options = array()) {
        echo 'ivs [command] [options]' . PHP_EOL;
        echo PHP_EOL;
        echo ' Command: ' . PHP_EOL;
        echo PHP_EOL;
        echo '     stat      Status information between snapshot and current workdir' . PHP_EOL;
        echo '     snapshot  Make a current shapshot' . PHP_EOL;
        echo '     deploy    Deploy current change to patch directory' . PHP_EOL;
        echo '     duplicate Make duplicate file list' . PHP_EOL;
        echo PHP_EOL;
        echo 'Copyright (C) 2012 by Vitold S. (internal usage only)' . PHP_EOL;
    }

    /**
     * Original entry point
     *
     * @return void
     */
    public function execute($params = array()) {
        $command = $params[0];
        $commandName = 'command' . ucfirst($command);
        if (method_exists($this, $commandName)) {
            $options = array_slice($params, 1);
            $this->{$commandName}($options);
        } else {
            echo 'Unknown command: "' . $command . '".' . PHP_EOL;
            exit(1);
        }
    }

}

// Executor prepare
global $argv;
$ivs = new InternalVersionSystem();
$params = array_slice($argv, 1);
$ivs->execute($params);
